package com.spring.demo8.eg8;

import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;

/**
 * @author vhans
 */
public class Test {

    @org.junit.Test
    public void test1() {
        DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
        // 添加一个InstantiationAwareBeanPostProcessor，重写postProcessAfterInstantiation方法
        // 当bean名称为user1时，不进行bean属性的赋值
        factory.addBeanPostProcessor(new InstantiationAwareBeanPostProcessor() {
            @Override
            public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
                return !"user1".equals(beanName);
            }
        });

        factory.registerBeanDefinition("user1", BeanDefinitionBuilder.genericBeanDefinition(User.class)
                .addPropertyValue("name", "Vhans")
                .addPropertyValue("age", 20)
                .getBeanDefinition());

        factory.registerBeanDefinition("user2", BeanDefinitionBuilder.genericBeanDefinition(User.class)
                .addPropertyValue("name", "luowei")
                .addPropertyValue("age", 20)
                .getBeanDefinition());

        for (String beanName : factory.getBeanDefinitionNames()) {
            System.out.println(beanName + "->" + factory.getBean(beanName));
        }
    }

    @org.junit.Test
    public void test2() {
        DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
        // 添加一个InstantiationAwareBeanPostProcessor，重写postProcessProperties方法
        // 当bean名称为user1时，给出相应属性
        factory.addBeanPostProcessor(new InstantiationAwareBeanPostProcessor() {
            @Override
            public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
                if ("user1".equals(beanName)) {
                    MutablePropertyValues mpvs = (MutablePropertyValues) pvs;
                    // 设置属性信息
                    mpvs.add("name", "路人");
                    mpvs.add("age", 30);
                }
                return null;
            }
        });

        factory.registerBeanDefinition("user1", BeanDefinitionBuilder.genericBeanDefinition(User.class)
                .addPropertyValue("name", "luowei")
                .getBeanDefinition());

        factory.registerBeanDefinition("user2", BeanDefinitionBuilder.genericBeanDefinition(User.class)
                .addPropertyValue("name", "Vhans")
                .addPropertyValue("age", 20)
                .getBeanDefinition());

        for (String beanName : factory.getBeanDefinitionNames()) {
            System.out.println(beanName + "->" + factory.getBean(beanName));
        }
    }
}
